package com.wwh.wwhspringmvc.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.File;
import java.net.URL;

/**
 * @author Albert
 * @date 2024/9/5-18:45
 * @describe 用于解析Spring的配置文件
 */

public class XMLParser {
    public static String getBasePackage(String xmlFile) {
        SAXReader saxReader = new SAXReader();
        URL resource = XMLParser.class.getClassLoader().getResource(xmlFile);
        Document document = null;
        try {
            document = saxReader.read(new File(resource.getFile()));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element element = document.getRootElement().element("component-scan");
        String basePackage = element.attributeValue("base-package");
        return basePackage;
    }

    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        URL resource = XMLParser.class.getClassLoader().getResource("wwhspringmvc.xml");
        System.out.println(resource);
        Document document = saxReader.read(new File("/E:/wangyuan/java_project/www-springMVC/target/classes/wwhspringmvc.xml"));
        Element element = document.getRootElement().element("component-scan");
        String attributeValue = element.attributeValue("base-package");
        System.out.println(attributeValue);
        return;
    }
}
